#include "Platform.h"

Platform::Platform(void)
{
	numOfPlatformWidth = 1;
	numOfPlatformHeight = 1;

	platform = new Object*[numOfPlatformWidth];

	for(int i = 0; i < numOfPlatformWidth; ++i)
	{
		platform[i] = new Object[numOfPlatformHeight];
	}

	platform[0][0].InitSprite(1);
	platform[0][0].SetSprite(0, "images/Obstacle/path.tga");

	wall = new BoundingLine[3];
}

Platform::~Platform(void)
{
	if(platform != NULL)
	{
		for(int i = 0; i < numOfPlatformWidth; ++i)
			delete [] platform[i];

		platform = NULL;
	}

	if(wall != NULL)
	{
		delete [] wall;
		wall = NULL;
	}
}

void Platform::InitPlatformLength(Vector3D position, int numOfPlatformWidth, int numOfPlatformHeight, PATHTYPE type, float r, float g, float b)
{
	this->numOfPlatformWidth = numOfPlatformWidth;
	this->numOfPlatformHeight = numOfPlatformHeight;
	this->type = type;

	platform = new Object*[this->numOfPlatformWidth];

	for(int i = 0; i < this->numOfPlatformWidth; ++i)
	{
		platform[i] = new Object[this->numOfPlatformHeight];
	}

	if(type == GRASS)
	{
		for(int x = 0; x < this->numOfPlatformWidth; ++x)
		{
			for(int y = 0; y < this->numOfPlatformHeight; ++y)
			{
				platform[x][y].InitSprite(1);
				platform[x][y].SetSprite(0, "images/IngameScene/SeamlessFloor.tga");

				platform[x][y].Init(Vector3D(position.x, position.y, 0), 140, 75);
				platform[x][y].SetPosition(Vector3D(position.x + x * platform[x][y].GetWidth(), position.y - y * platform[x][y].GetHeight(), 0));
				platform[x][y].SetColor(r, g, b, 1.0f);

				platform[x][y].SetOnGround(true);
			}
		}
	}
	else if(type == HAY)
	{
		for(int x = 0; x < this->numOfPlatformWidth; ++x)
		{
			for(int y = 0; y < this->numOfPlatformHeight; ++y)
			{
				platform[x][y].InitSprite(1);
				platform[x][y].SetSprite(0, "images/IngameScene/Hay.tga");

				platform[x][y].Init(Vector3D(position.x, position.y, 0), 140, 75);
				platform[x][y].SetPosition(Vector3D(position.x + x * platform[x][y].GetWidth(), position.y - y * platform[x][y].GetHeight(), 0));
				platform[x][y].SetColor(r, g, b, 1.0f);

				platform[x][y].SetOnGround(true);
			}
		}
	}
	else if(type == TREE)
	{
		for(int x = 0; x < this->numOfPlatformWidth; ++x)
		{
			for(int y = 0; y < this->numOfPlatformHeight; ++y)
			{
				platform[x][y].InitSprite(1);
				platform[x][y].SetSprite(0, "images//IngameScene/Tree.tga");

				platform[x][y].Init(Vector3D(position.x, position.y, 0), 140, 175);
				platform[x][y].SetPosition(Vector3D(position.x + x * platform[x][y].GetWidth(), position.y - y * platform[x][y].GetHeight(), 0));
				platform[x][y].SetColor(r, g, b, 1.0f);

				platform[x][y].SetOnGround(true);
			}
		}
	}
	else if(type == LAMP)
	{
		for(int x = 0; x < this->numOfPlatformWidth; ++x)
		{
			for(int y = 0; y < this->numOfPlatformHeight; ++y)
			{
				platform[x][y].InitSprite(1);
				platform[x][y].SetSprite(0, "images//IngameScene/Lamp.tga");

				platform[x][y].Init(Vector3D(position.x, position.y, 0), 140, 275);
				platform[x][y].SetPosition(Vector3D(position.x + x * platform[x][y].GetWidth(), position.y - y * platform[x][y].GetHeight(), 0));
				platform[x][y].SetColor(r, g, b, 1.0f);

				platform[x][y].SetOnGround(true);
			}
		}
	}
	else if(type == AISPAWNPOINT)
	{
		for(int x = 0; x < this->numOfPlatformWidth; ++x)
		{
			for(int y = 0; y < this->numOfPlatformHeight; ++y)
			{
				platform[x][y].InitSprite(1);
				platform[x][y].SetSprite(0, "images//IngameScene/AI_SpawnPoint.tga");

				platform[x][y].Init(Vector3D(position.x, position.y, 0), 80, 80);
				platform[x][y].SetPosition(Vector3D(position.x + x * platform[x][y].GetWidth(), position.y - y * platform[x][y].GetHeight(), 0));
				platform[x][y].SetColor(r, g, b, 1.0f);

				platform[x][y].SetOnGround(true);
			}
		}
	}else if(type == SPECIALSPAWN)
	{
		for(int x = 0; x < this->numOfPlatformWidth; ++x)
		{
			for(int y = 0; y < this->numOfPlatformHeight; ++y)
			{
				platform[x][y].InitSprite(1);
				platform[x][y].SetSprite(0, "images//IngameScene/AI_SpawnPoint.tga");

				platform[x][y].Init(Vector3D(position.x, position.y, 0), 80, 80);
				platform[x][y].SetPosition(Vector3D(position.x + x * platform[x][y].GetWidth(), position.y - y * platform[x][y].GetHeight(), 0));
				platform[x][y].SetColor(r, g, b, 1.0f);

				platform[x][y].SetOnGround(true);
			}
		}
	}

	if(type == BOUNDARY)
	{
		for(int x = 0; x < this->numOfPlatformWidth; ++x)
		{
			for(int y = 0; y < this->numOfPlatformHeight; ++y)
			{
				platform[x][y].InitSprite(1);
				platform[x][y].SetSprite(0, "images/IngameScene/AI_SpawnPoint.tga");

				platform[x][y].Init(Vector3D(position.x, position.y, 0), 80, 80);
				platform[x][y].SetPosition(Vector3D(position.x + x * platform[x][y].GetWidth(), position.y - y * platform[x][y].GetHeight(), 0));
				platform[x][y].SetColor(r, g, b, 1.0f);

				platform[x][y].SetOnGround(true);
			}
		}
	}

	platform[0][0].InitLine(platform[0][this->numOfPlatformHeight - 1].GetPosition()
							, Vector3D(platform[this->numOfPlatformWidth - 1][this->numOfPlatformHeight - 1].GetPosition().x + platform[this->numOfPlatformWidth - 1][this->numOfPlatformHeight - 1].GetWidth(), platform[this->numOfPlatformWidth - 1][this->numOfPlatformHeight - 1].GetPosition().y, 0));

	wall[0].InitLine(Vector3D(platform[0][0].GetPosition().x, platform[this->numOfPlatformWidth - 1][this->numOfPlatformHeight - 1].GetPosition().y, 0)
					, Vector3D(platform[0][0].GetPosition().x, platform[0][0].GetPosition().y + platform[0][this->numOfPlatformHeight - 1].GetHeight(), 0));

	wall[1].InitLine(Vector3D(platform[this->numOfPlatformWidth - 1][0].GetPosition().x + platform[this->numOfPlatformWidth - 1][0].GetWidth(), platform[this->numOfPlatformWidth - 1][this->numOfPlatformHeight - 1].GetPosition().y, 0)
					, Vector3D(platform[this->numOfPlatformWidth - 1][0].GetPosition().x + platform[this->numOfPlatformWidth - 1][0].GetWidth(), platform[0][0].GetPosition().y + platform[0][this->numOfPlatformHeight - 1].GetHeight(), 0));

	wall[2].InitLine(Vector3D(platform[0][0].GetPosition().x, platform[0][0].GetPosition().y + platform[0][0].GetHeight(), 0)
							, Vector3D(platform[this->numOfPlatformWidth - 1][0].GetPosition().x + platform[this->numOfPlatformWidth - 1][0].GetWidth(), platform[0][0].GetPosition().y + platform[0][0].GetHeight(), 0));
}

void Platform::DrawPlatform(void)
{
	for(int x = 0; x < this->numOfPlatformWidth; ++x)
	{
		for(int y = 0; y < this->numOfPlatformHeight; ++y)
		{
			platform[x][y].RenderObject();
		}
	}

	/*wall[0].DrawLine();
	wall[1].DrawLine();
	wall[2].DrawLine();*/
}

void Platform::SetPosition(Vector3D position)
{
	for(int x = 0; x < this->numOfPlatformWidth; x++)
	{
		for(int y = 0; y < this->numOfPlatformWidth; y++)
		{
			platform[x][y].SetPosition(Vector3D(position.x + x * platform[x][y].GetWidth(), position.y - y * platform[x][y].GetHeight(), 0));
		}
	}
}

Vector3D Platform::GetPosition(void)
{
	return Vector3D(platform[0][0].GetPosition().x + (platform[0][0].GetWidth() / 2), platform[0][0].GetPosition().y, platform[0][0].GetPosition().z);
}

Vector3D Platform::GetoOff(void)
{
	return platform[0][0].oOff;
}

void Platform::UpdatePlatform(Vector3D offset, Vector3D direction, float speed, float elapsed)
{
	for(int x = 0; x < this->numOfPlatformWidth; ++x)
	{
		for(int y = 0; y < this->numOfPlatformHeight; ++y)
		{
			platform[x][y].SetOnGround(true);

			platform[x][y].SetDirection(direction);
			platform[x][y].SetSpeed(speed);

			platform[x][y].UpdateObject(offset,elapsed);
		}
	}

	platform[0][0].InitLine(platform[0][this->numOfPlatformHeight - 1].GetPosition()
							, Vector3D(platform[this->numOfPlatformWidth - 1][this->numOfPlatformHeight - 1].GetPosition().x + platform[this->numOfPlatformWidth - 1][this->numOfPlatformHeight - 1].GetWidth(), platform[this->numOfPlatformWidth - 1][this->numOfPlatformHeight - 1].GetPosition().y, 0));

	wall[0].InitLine(Vector3D(platform[0][0].GetPosition().x, platform[this->numOfPlatformWidth - 1][this->numOfPlatformHeight - 1].GetPosition().y, 0)
					, Vector3D(platform[0][0].GetPosition().x, platform[0][0].GetPosition().y + platform[0][this->numOfPlatformHeight - 1].GetHeight(), 0));

	wall[1].InitLine(Vector3D(platform[this->numOfPlatformWidth - 1][0].GetPosition().x + platform[this->numOfPlatformWidth - 1][0].GetWidth(), platform[this->numOfPlatformWidth - 1][this->numOfPlatformHeight - 1].GetPosition().y, 0)
					, Vector3D(platform[this->numOfPlatformWidth - 1][0].GetPosition().x + platform[this->numOfPlatformWidth - 1][0].GetWidth(), platform[0][0].GetPosition().y + platform[0][this->numOfPlatformHeight - 1].GetHeight(), 0));

	wall[2].InitLine(Vector3D(platform[0][0].GetPosition().x, platform[0][0].GetPosition().y + platform[0][0].GetHeight(), 0)
							, Vector3D(platform[this->numOfPlatformWidth - 1][0].GetPosition().x + platform[this->numOfPlatformWidth - 1][0].GetWidth(), platform[0][0].GetPosition().y + platform[0][0].GetHeight(), 0));
}

Platform::COLLISIONTYPE Platform::CollisionWithPlatform(BoundingBox &theBox)
{
	//for(int i = 0; i < numberOfPlatform; i++){
	//	if(platform[i].GetBoundingBox().HitTest(theBox)){
	//		//cout << "Hit" << endl;
	//		return true;
	//	}
	//}
	if(type != AISPAWNPOINT || type != SPECIALSPAWN)
	{
		if(platform[0][0].GetBoundingLine().HitTest(theBox))
		{
			return FLOOR;
		}

		if(wall[2].HitTest(theBox) && type != HAY){
			return CEILING;
		}

		for(int i = 0; i < 2; i++)
		{
			if(wall[i].HitTest(theBox) && type != HAY)
			{
				return WALL;
			}
		}
	}

	return NOTHING;

	//return false;
}